using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security.Cryptography;
using System.Text;

/// <summary>
/// Summary description for RSACryto
/// </summary>
public class RSACryto
{
	public RSACryto()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public string Encrypt(string inputString, string priKey)
    {
        try
        {
            ASCIIEncoding ByteConverter = new ASCIIEncoding();
            byte[] HashValue = ByteConverter.GetBytes(inputString);
            RSACryptoServiceProvider rsapro = new RSACryptoServiceProvider();
            rsapro.FromXmlString(priKey);
            byte[] signvalue = rsapro.SignData(HashValue, new SHA1CryptoServiceProvider());
            string sRET = ByteToHexString(signvalue);
            return sRET;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }

    public bool Decrypt(string inputString, string receiveString, string publicKey)
    {
        bool bRet = false;
        try
        {
            RSACryptoServiceProvider rsapro = new RSACryptoServiceProvider();
            byte[] HasValue = Encoding.UTF8.GetBytes(inputString);
            byte[] SignHashValue = ConvertHexStringToByte(receiveString); //Convert.FromBase64String(_SignRecei);
            rsapro.FromXmlString(publicKey);
            bool bVery = rsapro.VerifyData(HasValue, "SHA1", SignHashValue);
            if (bVery)
                bRet = true;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
        return bRet;
    }
    private string ByteToHexString(byte[] data)
    {
        try
        {
            StringBuilder hexString = new StringBuilder(data.Length);
            for (int i = 0; i < data.Length; i++)
            {
                hexString.Append(data[i].ToString("X2"));
            }
            return hexString.ToString();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }
    private byte[] ConvertHexStringToByte(string hex)
    {
        try
        {
            int NumberChars = hex.Length;
            byte[] bytes = new byte[NumberChars / 2];
            for (int i = 0; i < NumberChars; i += 2)
                bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
            return bytes;

        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }
}
public class TripleDes
{
    public static string Encrypt(string key, string data)
    {
        data = data.Trim();
        byte[] keydata = Encoding.ASCII.GetBytes(key);
        string md5String = BitConverter.ToString(new
    MD5CryptoServiceProvider().ComputeHash(keydata)).Replace("-", "").ToLower();
        byte[] tripleDesKey = Encoding.ASCII.GetBytes(md5String.Substring(0, 24));
        TripleDES tripdes = TripleDESCryptoServiceProvider.Create();
        tripdes.Mode = CipherMode.ECB;
        tripdes.Key = tripleDesKey;
        tripdes.GenerateIV();
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        CryptoStream encStream = new CryptoStream(ms, tripdes.CreateEncryptor(),
        CryptoStreamMode.Write);
        encStream.Write(Encoding.ASCII.GetBytes(data), 0,
        Encoding.ASCII.GetByteCount(data));
        encStream.FlushFinalBlock();
        byte[] cryptoByte = ms.ToArray();
        ms.Close();
        encStream.Close();
        return Convert.ToBase64String(cryptoByte, 0, cryptoByte.GetLength(0)).Trim();
    }
    public static string Decrypt(string sessionid, string dataen)
    {
        byte[] keydata = Encoding.ASCII.GetBytes(sessionid);
        string md5String = BitConverter.ToString(new
            MD5CryptoServiceProvider().ComputeHash(keydata)).Replace("-", "").ToLower();
        byte[] tripleDesKey = Encoding.ASCII.GetBytes(md5String.Substring(0, 24));
        TripleDES tripdes = TripleDESCryptoServiceProvider.Create();
        tripdes.Mode = CipherMode.ECB;
        tripdes.Key = tripleDesKey;
        tripdes.GenerateIV();
        ICryptoTransform ict = tripdes.CreateDecryptor();
        byte[] toDycrypt = Convert.FromBase64String(dataen);
        byte[] cryptoByte = ict.TransformFinalBlock(toDycrypt, 0, toDycrypt.Length);

        string data = Convert.ToBase64String(cryptoByte, 0, cryptoByte.GetLength(0)).Trim();
        return Encoding.ASCII.GetString(Convert.FromBase64String(data));

    }
}